package com.itany.order.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.itany.order.service.IRoderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
@com.alibaba.dubbo.config.annotation.Service
public class OrderServiceImpl implements IRoderService {
    @Autowired
    private AlipayClient alipayClient;


    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public String getQrCode() {
        String qrCode ="";
        try{
            //生成唯一单号:数据库查询订单表
            //假数据
            String no = System.currentTimeMillis()+"";
            //声明请求对象
            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
            //声明Model对象
            AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
            model.setOutTradeNo(no);//单号
            model.setTotalAmount("0.01");//价格
            model.setSubject("剑来");//订单主体：订单表订单号
            //回调地址
            request.setNotifyUrl("");
            request.setBizModel(model);
            //执行
            AlipayTradePrecreateResponse response = alipayClient.execute(request);
            //判断响应是否有成功
            if(response.isSuccess()){
                System.out.println("success");
                qrCode=response.getQrCode();
                System.out.println(qrCode);
            }else{
                System.out.println("error");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return qrCode;
    }
}
